﻿(function(){
if(!window.ksUI){
	ksUI = {};
}
if(!ksUI.Suggest){
	ksUI.Suggest =  {};
}
	var TimeOutID = null;
	ksUI.Suggest = function(oid,url){
		this.oid = oid;
		this.url = url;
		this._createSuggestBox()
	}
	ksUI.Suggest.prototype = {
		_createSuggestBox:function(){
			if(document.getElementById("ks-ui-suggest")){
				this.$suggestbox = $("#ks-ui-suggest");
				this.$suggestinner = $("#ks-ui-suggest-inner");
				this.$suggestlist =$("#ks-ui-suggest-list");
				this.$suggestsearch = $("#ks-ui-suggest-search");
			}else{
			this.$suggestbox = $(document.createElement("div")).addClass("ks-ui-suggest").attr("id","ks-ui-suggest").appendTo("body").hide();
			this.$suggestinner = $(document.createElement("div")).addClass("ks-ui-suggest-inner").attr("id","ks-ui-suggest-inner").appendTo(this.$suggestbox)
			this.$suggestlist = $(document.createElement("div")).attr("id","ks-ui-suggest-list").appendTo(this.$suggestinner).css("width","100%");
			this.$suggestsearch = $("<div class='ks-ui-suggest-search' style='text-align:right'  id='ks-ui-suggest-search'><input type='text' class='ks-ui-suggest-inputtext' id='ks-ui-suggest-inputtext' style='display:none' /><button type='button'class='blue' style='display:none' id='ks-ui-suggest-list-search'><span></span>查询</button><button type='button' id='ks-ui-suggest-list-close' class='normal'><span></span>关闭</button></div>").appendTo(this.$suggestinner)
			}	
			this.$suggestlist.html("<div class='ks-ui-suggest-help'>数据加载中……</div>")
			},
		CloseSuggext:function(){
			this.$suggestlist.html("<div class='ks-ui-suggest-help'>数据加载中……</div>");
			this.$suggestbox.hide();
			window.clearTimeout(TimeOutID);
		},
		_ShowSuggext:function(){
			var _offset = this.$suggestinput.offset();
			var _this= this;
			this.$suggestbox.css({
					left:_offset.left,
					top:_offset.top+30
				}).show()
		},
		BindInputEevents:function(){
			var _this = this;
			this.$suggestinput = $("#"+_this.oid);
			this.$suggestinput.unbind()
			this.$suggestinput.focus(function(e){
					_this._ShowSuggext(e)
					_this._keyupHandler(e)
				}).keyup(function(e){
							_this._keyupHandler(e)
						});
			$("#ks-ui-suggest-list-search").unbind()
			$("#ks-ui-suggest-list-search").click(function(){
					_this._sendRequest($("#ks-ui-suggest-inputtext").val())
				});
			$("#ks-ui-suggest-list-close").unbind()
			$("#ks-ui-suggest-list-close").click(function(){
					_this.CloseSuggext()
				});
		},
		_BindItemEvents:function(){
			var _this = this;
		//	this.$suggestlist.unbind()
			this.$suggestlist.find(".ks-ui-suggest-item").hover(function(){
					$(this).addClass("ks-ui-suggest-item-hover")
				},function(){
					$(this).removeClass("ks-ui-suggest-item-hover")
					}).mousedown(function(){
						_this.$suggestinput.val($(this).attr("value"))
						_this.CloseSuggext()
							
					})
		},
		_keyupHandler:function(e){
			var _this = this;
			window.clearTimeout(TimeOutID);
				if(!_this._specialKeys(e)){
			TimeOutID = window.setTimeout(function(){
							_this._sendRequest(_this.$suggestinput.val())
				},250)
				}
			},
		_sendRequest:function(value){
			var _this = this;
			var _data = "zkzh="+encodeURI(value)+"&s="+Math.random();
			$.get(_this.url,_data,function(data){
					_this.$suggestlist.html(_this._formatText(data));
					_this._BindItemEvents();
					_this._resizeList();
				})
		},
		//检测是不是点击了特殊按键
		_specialKeys:function(e){
				var enterKey = 13;
				var upArrow = 38;
				var downArrow = 40;
				
				if(e.keyCode == upArrow || e.keyCode == downArrow){
					return true
				}
				else{
					return false;
				}
				return false;
			},
			//格式化返回结果
			_formatText:function(text){
				var item = ""
				if($.trim(text)==""){
					item +="<div class='ks-ui-suggest-empty'>没有你要查询的信息</div>"	
				}
				else{
				var text = text.split("|");
				for(var i=0;i<text.length;i++){
					var _text = $.trim(text[i].split(",")[0]);
					var _name = $.trim(text[i].split(",")[1]);
					if(_text){
						if(i==text.length){
							item +="<div value='"+_text+"'  class='ks-ui-suggest-item ks-ui-suggest-item-last'><span>"+_name+"</span>"+_text+"</div>"
						}else{
							item +="<div value='"+_text+"'  class='ks-ui-suggest-item '><span>"+_name+"</span>"+_text+"</div>"
						}
						
					}
				}
				}
				return item;
			},
			_resizeList:function(){
				var _list = this.$suggestlist;
				if(_list.find(".ks-ui-suggest-item").length>=10){
					_list.css({
							"overflow":"auto",
							height:10*20
						})
				}else{
					_list.css({
							height:"100%"
						})
				}
			}
	}



	
})()